/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.bean;

import client.dal.AccountDAL;
import client.dal.WorkerDAL;
import client.entity.Account;
import client.entity.Worker;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import util.EncryptionPassword;

/**
 *
 * @author mrlucky
 */
@ManagedBean
@SessionScoped
public class WorkerSignUp implements Serializable {

    private Account acc;
    private Worker worker;
    private String confirmPass;
    private String pass_temp;
    private String gender_temp = "1";

    public String getPass_temp() {
        return pass_temp;
    }

    public void setPass_temp(String pass_temp) {
        this.pass_temp = pass_temp;
    }

    public String getGender_temp() {
        return gender_temp;
    }

    public void setGender_temp(String gender_temp) {
        this.gender_temp = gender_temp;
    }

    public String getConfirmPass() {
        return confirmPass;
    }

    public void setConfirmPass(String confirmPass) {
        this.confirmPass = confirmPass;
    }

    public Account getAcc() {
        if (acc == null) {
            acc = new Account();
        }
        return acc;
    }

    public void setAcc(Account acc) {
        this.acc = acc;
    }

    public Worker getWorker() {
        if (worker == null) {
            worker = new Worker();
        }
        return worker;
    }

    public void setWorker(Worker worker) {
        this.worker = worker;
    }

    public WorkerSignUp() {
    }

    public WorkerSignUp(Account acc, Worker worker, String confirmPass) {
        this.acc = acc;
        this.worker = worker;
        this.confirmPass = confirmPass;
    }

    /**
     * Creates a new instance of WorkerSignUp
     */
    public String insertWorkerSignUp() {
        if (new AccountDAL().loadAccountByUserName(acc.getUseName()) == null) {
            String hashPass = new EncryptionPassword().Encryption(acc.getPassword());
            Account a = new Account(acc.getUseName(), hashPass, Boolean.FALSE);
            boolean gen = false;
            if (gender_temp.equals("1")) {
                gen = true;
                gender_temp = "Male";
            } else {
                gender_temp = "Female";
            }
            worker.setAccount(a);
            worker.setGender(gen);
            worker.setStatus(Boolean.FALSE);
            new AccountDAL().insertAccount(a);
            new WorkerDAL().insertWorker(worker);
            pass_temp = "";
            for (int i = 0; i < acc.getPassword().length(); i++) {
                pass_temp += "*";
            }
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("account", a);
            return "workersignupsuccess.xhtml?faces-redirect=true";
        } else {
            FacesContext.getCurrentInstance().addMessage("form1:useName", new FacesMessage("* User name is used"));
            return "";
        }
    }

    public String signUpSuccess() {
        Account a = new AccountDAL().loadAccountByUserName(acc.getUseName());
        return "createCVStep1.xhtml?accId=" + a.getId() + "&faces-redirect=true";
        //return "createCVStep1.xhtml?accId=19&faces-redirect=true";
    }
}
